package com.gwcm.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwcm.exception.BusinessException;
import com.gwcm.model.dto.memberCard.MemberCardQueryRequest;
import com.gwcm.model.entity.MemberCard;
import com.gwcm.model.vo.MemberCardVo;
import com.gwcm.service.MemberCardService;
import com.gwcm.mapper.MemberCardMapper;
import com.gwcm.util.PageUtil;
import com.yhz.commonutil.common.ErrorCode;
import com.yhz.commonutil.constant.SortConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
* @author 15375
* @description 针对表【member_card】的数据库操作Service实现
* @createDate 2023-03-21 16:19:38
*/
@Service
public class MemberCardServiceImpl extends ServiceImpl<MemberCardMapper, MemberCard> implements MemberCardService{
    @Resource
    private MemberCardMapper memberCardMapper;
    /**
     * 会员卡分页查询
     * @param memberCardQueryRequest 查询条件
     * @return
     * @throws BusinessException
     */
    @Override
    public IPage<MemberCardVo> pageList(MemberCardQueryRequest memberCardQueryRequest) throws BusinessException {
        IPage<MemberCard> page = PageUtil.vaildPageParam(memberCardQueryRequest.getCurrent(), memberCardQueryRequest.getPageSize());
        String sortField = memberCardQueryRequest.getSortField();
        String sortOrder = memberCardQueryRequest.getSortOrder();

        if (StrUtil.isBlank(sortOrder) || !StrUtil.equalsAny(sortOrder, SortConstant.SORT_ORDER_ASC, SortConstant.SORT_ORDER_DESC)) {
            //默认按升序排序
            sortOrder = SortConstant.SORT_ORDER_ASC;
        }
        memberCardQueryRequest.setSortField(sortField);
        memberCardQueryRequest.setSortOrder(sortOrder);
        IPage<MemberCardVo> memberCardToIPage = memberCardMapper.selectMemberCardToPage(page, memberCardQueryRequest);
        MemberCard memberCard = getMemberCardByCardNameVaildMonth(memberCardQueryRequest);
        IPage<MemberCardVo> memberCardVoIPage = memberCardToIPage.convert(item -> {
            MemberCardVo memberCardVo = new MemberCardVo();
            BeanUtils.copyProperties(item, memberCardVo);
            return memberCardVo;
        });

        return memberCardVoIPage;
    }

    /**
     * 新增会员卡
     * @param memberCard
     */
    @Override
    public void saveMemberCard(MemberCard memberCard) {
        int insert = baseMapper.insert(memberCard);
        if (insert < 1) {
            throw new BusinessException(ErrorCode.SAVE_ERROR);
        }
    }

    /**
     * 修改会员卡
     * @param memberCard
     */
    @Override
    public void updateMemberCard(MemberCard memberCard) {
        String memberCardName = memberCard.getCardName();
        QueryWrapper<MemberCard> memberCardQueryWrapper = new QueryWrapper<>();
        memberCardQueryWrapper.eq("card_name",memberCardName);
        int update = baseMapper.updateById(memberCard);
        if (update < 1) {
            throw new BusinessException(ErrorCode.UPDATE_ERROR);
        }
    }

    /**
     * 通过CardName和ValidMonth获取会员卡信息
     * @param memberCardQueryRequest
     * @return
     */
    public MemberCard getMemberCardByCardNameVaildMonth(MemberCardQueryRequest memberCardQueryRequest){
        String memberCardName = memberCardQueryRequest.getCardName();
        int validMonth = memberCardQueryRequest.getValidMonth();
        int validTime = validMonth * 30;
        QueryWrapper<MemberCard> memberCardQueryWrapper = new QueryWrapper<>();
        memberCardQueryWrapper.eq("card_name",memberCardName).eq("valid_time",validTime);
        MemberCard memberCard = baseMapper.selectOne(memberCardQueryWrapper);
        return memberCard;
    }

}




